package com.edu.internship.busi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.internship.busi.dto.PermissionDTO;
import com.edu.internship.busi.dto.RoleDTO;
import com.edu.internship.busi.entity.Permission;
import com.edu.internship.busi.entity.Role;
import com.edu.internship.busi.mapper.PermissionMapper;
import com.edu.internship.busi.mapper.RoleMapper;
import com.edu.internship.busi.service.IPermissionService;
import com.edu.internship.busi.service.IRoleService;
import com.edu.internship.config.cache.GuavaCache;
import com.edu.internship.glabal.constants.Constants;
import com.edu.internship.glabal.constants.ResultCodeEnum;
import com.edu.internship.glabal.exception.BusinessException;
import com.edu.internship.util.BeanExUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Resource
    private GuavaCache guavaCache;

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public boolean created(PermissionDTO dto) {
        Permission permission = this.nonExistsAndConvert(dto);

        return super.save(permission);
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public boolean update(PermissionDTO dto) {
        Permission db = super.getById(dto.getId());
        guavaCache.del(Constants.RedisKeyCons.DICTIONARY.getCode() + db.getName());
        Permission permission;
        if (!Objects.equals(db.getId(), dto.getId())) {
            permission = this.nonExistsAndConvert(dto);
        } else {
            permission = BeanExUtils.convertTo(dto, Permission::new);
        }
        super.updateById(permission);
        return false;
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public boolean remove(String id) {
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public List<Permission> change(List<String> id) {
        return super.listByIds(id);

    }

    private Permission nonExistsAndConvert(PermissionDTO dto) {
        LambdaQueryWrapper<Permission> qw = Wrappers.lambdaQuery();
        qw.eq(Permission::getId, dto.getId());
        if (super.count(qw) > 0) {
            throw new BusinessException(ResultCodeEnum.DATABASE_ERROR,"当前ID已存在，请重新添加");
        }
        LambdaQueryWrapper<Permission> re = Wrappers.lambdaQuery();
        re.eq(Permission::getName,dto.getName());
        if (super.count(re) > 0) {
            throw new BusinessException(ResultCodeEnum.USE_NAME_EN_EXISTS,"当前角色名已存在，请重新添加");
        }
        return BeanExUtils.convertTo(dto,Permission::new);
    }
}
